var urlParams = {
    loginAction: webRoot + "/login/login",
    regAction: webRoot + "/user/reg"
};

$(function () {
    initEvent();
});

/**
 * 添加监听
 */
var tab = 'account_number';

function initEvent() {
    // 选项卡切换
    $(".account_number").click(function () {
        $('.tel-warn').addClass('hide');
        tab = $(this).attr('class').split(' ')[0];
        checkBtn();
        $(this).addClass("on");
        $(".message").removeClass("on");
        $(".form2").addClass("hide");
        $(".form1").removeClass("hide");
    });
    // 选项卡切换
    $(".message").click(function () {
        $('.tel-warn').addClass('hide');
        tab = $(this).attr('class').split(' ')[0];
        checkBtn();
        $(this).addClass("on");
        $(".account_number").removeClass("on");
        $(".form2").removeClass("hide");
        $(".form1").addClass("hide");
    });

    $('#num').keyup(function (event) {
        $('.tel-warn').addClass('hide');
        checkBtn();
    });

    $('#pass').keyup(function (event) {
        $('.tel-warn').addClass('hide');
        checkBtn();
    });

    $('#veri').keyup(function (event) {
        $('.tel-warn').addClass('hide');
        checkBtn();
    });

    $('#num2').keyup(function (event) {
        $('.tel-warn').addClass('hide');
        checkBtn();
    });

    $('#veri-code').keyup(function (event) {
        $('.tel-warn').addClass('hide');
        checkBtn();
    });
}

// 按钮是否可点击
function checkBtn() {
    $(".log-btn").off('click');
    if (tab == 'account_number') {
        var inp = $.trim($('#num').val());
        var pass = $.trim($('#pass').val());
        if (inp != '' && pass != '') {
            if (!$('.code').hasClass('hide')) {
                code = $.trim($('#veri').val());
                if (code == '') {
                    $(".log-btn").addClass("off");
                } else {
                    $(".log-btn").removeClass("off");
                    sendBtn();
                }
            } else {
                $(".log-btn").removeClass("off");
                sendBtn();
            }
        } else {
            $(".log-btn").addClass("off");
        }
    } else {
        var phone = $.trim($('#num2').val());
        var code2 = $.trim($('#veri-code').val());
        if (phone != '' && code2 != '') {
            $(".log-btn").removeClass("off");
            sendBtn();
        } else {
            $(".log-btn").addClass("off");
        }
    }
}

// 登录点击事件
function sendBtn() {
    if (tab == 'account_number') {
        $(".log-btn").click(function () {
            // var type = 'phone';
            var inp = $.trim($('#num').val());
            var pass = $.md5($.trim($('#pass').val()));
            var checkPhone = MatchesClass.checkPhone(inp);
            var checkPass = MatchesClass.checkPass($.trim($('#pass').val()));
            if (inp == "admin") {
                checkPhone = "success";
            }
            if (checkPhone != "success") {
                alert(checkPhone);
                return;
            } else if (checkPass != "success") {
                alert(checkPass);
                return;
            }
            $.ajax({
                type: 'post',
                url: urlParams.loginAction,
                cache: false,
                data: {phoneNumber: inp, password: pass},
                dataType: 'json',
                success: function (data) {
                    if (data && data.id) {
                        if (data.userName == "admin") {
                            window.location.href = webRoot + "/pages/admin/adminMain.jsp";
                        } else {
                            window.location.href = webRoot;
                        }

                    } else {
                        alert("用户名或密码错误");
                    }
                },
                error: function () {
                }
            });
        });
    } else {
        $(".log-btn").click(function () {
            // var type = 'phone';
            var phone = $.trim($('#num2').val());
            var pcode = $.trim($('#veri-code').val());
            if (checkPhone(phone) && checkPass(pcode)) {
                $.ajax({
                    url: '/plogin',
                    type: 'post',
                    dataType: 'json',
                    async: true,
                    data: {phone: phone, code: pcode},
                    success: function (data) {
                        if (data.code == '0') {
                            // globalTip({'msg':'登录成功!','setTime':3,'jump':true,'URL':'http://www.ui.cn'});
                            globalTip(data.msg);
                        } else if (data.code == '1') {
                            $(".log-btn").off('click').addClass("off");
                            $('.num2-err').removeClass('hide').text(data.msg);
                            return false;
                        } else if (data.code == '2') {
                            $(".log-btn").off('click').addClass("off");
                            $('.error').removeClass('hide').text(data.msg);
                            return false;
                        }
                    },
                    error: function () {

                    }
                });
            } else {
                $(".log-btn").off('click').addClass("off");
                // $('.tel-warn').removeClass('hide').text('登录失败');
                return false;
            }
        });
    }
}